﻿/***********************************************************************************************************************************
 *    Name: LinkedServer.sql
 *  Author: Frank Figearo — http://www.sqlnerd.me/ — frank@sqlnerd.me
 * Summary: Add a new SQL Server linked server which is named differently than the server it targets.
**/
USE master;

:SETVAR  server OEMChatsworth
:SETVAR datasrc OEMChasSQL01\OEMSQL2005
:SETVAR catalog Null

BEGIN TRY EXECUTE dbo.sp_dropserver @server= N'$(server)', @droplogins= 'droplogins'; END TRY BEGIN CATCH END CATCH;
EXECUTE dbo.sp_addlinkedserver
	@srvproduct	= N'MSSQLServer',
	@provider	= N'SQLNCLI11',
	@server		= N'$(server)',
	@datasrc	= N'$(datasrc)',
	@catalog	= Null;
EXECUTE sys.sp_addlinkedsrvlogin @rmtsrvname= N'$(server)', @useself= N'True';
GO